package com.wisdom.weld.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtils {

    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;

    static {
        try {
            InputStream inputStream = ClassLoader.getSystemResourceAsStream("jdbc.properties");
            Properties prop = new Properties();
            prop.load(inputStream);
            URL = prop.getProperty("jdbc.url");
            USERNAME = prop.getProperty("jdbc.username");
            PASSWORD = prop.getProperty("jdbc.password");
        } catch (IOException e) {
            throw new RuntimeException("获取JDBC资源文件异常");
        }
    }

    /**
     * 获取数据库链接
     * @return 数据库链接
     */
    public static Connection getConnection() {
        try{
            Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            return connection;
        } catch (SQLException e) {
            throw new RuntimeException("");
        }
    }

    public static String getUserName(){
        return DBUtils.USERNAME;
    }


}
